package com.feiyu.universal.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feiyu.common.controller.BaseController;
import com.feiyu.universal.entity.Category;
import com.feiyu.universal.service.CategoryService;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tang ming
 * @since 2021-11-30
 */
@RestController
@RequestMapping("/groups")
@AllArgsConstructor
public class CategoryController{

    private CategoryService categoryService;

    @GetMapping("/detail/{id}")
    public R<Category> detail(@PathVariable int id){
        return R.data(categoryService.getById(id));
    }

    @GetMapping("/list/{id}")
    public R<List<Category>> list(@PathVariable("id") Long id){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("ownerId",id);
        return R.data(categoryService.list(wrapper));
    }
    @GetMapping("/page")
    public R<Page<Category>> page(Page<Category> page){
        return R.data(categoryService.page(page));
    }

    @PostMapping("/save")
    public R save(@RequestBody Category group){
        if (group.getId()==null||group.getId()==0)
            return R.status(categoryService.save(group));
        return R.status(categoryService.updateById(group));
    }

    @PostMapping("/update")
    public R update(@RequestBody Category group){
        return R.status(categoryService.updateById(group));
    }

    @PostMapping("/delete/{id}")
    public R delete(@PathVariable int id){
        return R.status(categoryService.removeById(id));
    }
}

